package org.wicket.jbpm.engine.workflow.pages;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.jbpm.graph.exe.Comment;
import org.jbpm.graph.exe.ProcessInstance;
import org.wicket.jbpm.engine.spring.JbpmLogEngine;
import org.wicket.jbpm.engine.workflow.WfTask;
import org.wicket.jbpm.page.WicketJBPMBasePage;
import org.wicket.jbpm.utils.DateFormatter;

/**
 * @author Emmanuel Nollase - emanux
 * created: Jun 4, 2009 - 2:58:58 PM
 */
public class JbpmProcessTaskPage extends WicketJBPMBasePage {

	@SpringBean private JbpmLogEngine jbpmLogEngine;
	
	public JbpmProcessTaskPage(final ProcessInstance instance) {
		
		final ListView taskProcess = new ListView("taskprocess", jbpmLogEngine.findAllTaskByProcessInstance(instance)){
			@Override
			protected void populateItem(ListItem item) {
				final WfTask task = (WfTask) item.getModelObject();
				item.setModel(new CompoundPropertyModel(task));
				item.add(new Label("name"));
				item.add(new Label("description"));
				item.add(new Label("endDt", DateFormatter.format(task.endDt) ));
				item.add(new Label("status",task.state.toString()));
				item.add(new ListView("comments",jbpmLogEngine.findAllComments(task.tid)){
					@Override
					protected void populateItem(ListItem item) {
						final Comment comment = (Comment) item.getModelObject();
						item.add(new Label("message",comment.getMessage()));
						item.add(new Label("dt",comment.getTime().toString()));
					}
				});
				
			}
		};
		
		add(taskProcess);
	}
}

